package com.example.a2032.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public static final String DefaultDateFormat = "yyyy.MM.dd HH:mm:ss";
    public static final String FullDateFormat = "yyyy.MM.dd HH:mm:ss SSS";
    public static final String DefaultDayFormatDay = "yyyy.MM.dd";
    public static final long oneDayMillis = 24 * 60 * 60 * 1000l;

    public static String getFormatDate(String format, Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * 精确到毫秒值
     *
     * @param date
     * @return
     */
    public static String getFullDefaultDate(Date date) {
        return getFormatDate(FullDateFormat, date);
    }

    public static String getDefaultDate(Date date) {
        return getFormatDate(DefaultDateFormat, date);
    }

    public static String getDefaultToday(Date date) {
        return getFormatDate(DefaultDayFormatDay, date);
    }

    public static long defaultFormatDate2Mil(String date) {
        return FormatDate2Mil(DefaultDateFormat, date);
    }

    public static long dayFormat2Mil(String date) {
        return FormatDate2Mil(DefaultDayFormatDay, date);
    }

    public static long fullFormatDate2Mil(String date) {
        return FormatDate2Mil(FullDateFormat, date);
    }

    public static String getTomorrowDateString(String today) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(dayFormat2Mil(today) + oneDayMillis);
        return getDefaultToday(c.getTime());
    }
    public static synchronized String getYesterdayDateString(String today) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(dayFormat2Mil(today) - oneDayMillis);
        return getDefaultToday(c.getTime());
    }
    public static long FormatDate2Mil(String format, String dateString) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 最后的数据比现在的日期还要大的时候，说明日期有错误;
     *
     * @param lastUpdate
     * @return
     */
    public static String getLastUploadDate(String lastUpdate) {
        long lastDate = defaultFormatDate2Mil(lastUpdate);
        long date = System.currentTimeMillis();
        return lastDate > date ? "2000.01.01 00:00:00" : lastUpdate;
    }
}
